#ifndef __JZM_ENC_API_T20_H__
#define __JZM_ENC_API_T20_H__
#include <stdint.h>
#include <stdbool.h>
//#include "helix_x264_enc.h"

#define EPRC_EN_API

#define write_reg(off, value) (*((volatile unsigned int *) (off)) = (value))
#define read_reg(off, value) (*((volatile unsigned int *) (off + value)))

typedef uint32_t JZ_U32;
typedef uint8_t  JZ_U8;
typedef int8_t   JZ_BOOL;
typedef int8_t   JZ_S8;
typedef int32_t  JZ_S32;
typedef float    JZ_FL;

#if 0
typedef enum{
    CBR   = 0,
    VBR   = 1,
    FIXQP = 2,
    SMART = 3
} VPU_RC_MODE_S_T20;

typedef enum{
    SF_DO_NOTHING   = 0,
    SF_DROP_CUR_FRM = 1,
    SF_RE_ENCODE    = 2
} VPU_RC_SuperFrmMode_S_T20;

typedef enum{
    DO_NOTHING      = 0,
    DROP_NEXT_FRM   = 1,
    PSKIP_NEXT_FRM  = 2
} VPU_RC_BitRateUpMode_S_T20;
#endif
typedef enum{
    STILL          = 0,
    MOVETOSTILL    = 1,
    STILLTOMOVE    = 2,
    MOVE           = 3,
    SHARPMOVE      = 4
} VPU_RC_SCENE_CONDITION_S;

typedef struct JZ_VPU_RECT_S_T20 {
    JZ_U32 lmbx;
    JZ_U32 rmbx;
    JZ_U32 umby;
    JZ_U32 bmby;
}JZ_VPU_RECT_S_T20;

typedef struct JZ_VPU_ROI_SINGLE_S_T20 {
    JZ_U8 en;
    JZ_U8 md;
    JZ_U8 roi_qp;
    JZ_VPU_RECT_S_T20 rect;
}JZ_VPU_ROI_SINGLE_S_T20;

typedef struct JZ_VPU_ROI_CFG_S_T20 {
    JZ_U8 base_qp;
    JZ_U8 max_qp;
    JZ_VPU_ROI_SINGLE_S_T20 roi[8];
}JZ_VPU_ROI_CFG_S_T20;

typedef struct JZ_VPU_SDE_CFG_S_T20 {
    JZ_U8 *state;
    unsigned int bs;             /*BS output address*/
    JZ_U8 qp;
    JZ_U8 skip_en;
}JZ_VPU_SDE_CFG_S_T20;

typedef struct JZ_VPU_GLB_CFG_S_T20 {
    JZ_U8 slice_type;
    JZ_U8 mb_width;
    JZ_U8 mb_height;
    JZ_U8 first_mby;
    JZ_U8 last_mby;  //for multi-slice
}JZ_VPU_GLB_CFG_S_T20;

typedef struct JZ_VPU_VMAU_CFG_S_T20 {
    JZ_U8 scaling_list[4][16];
}JZ_VPU_VMAU_CFG_S_T20;

typedef struct JZ_VPU_DBLK_CFG_S_T20 {
    JZ_U8 dblk_en;
    unsigned int rotate;
    JZ_U8 alpha_c0_offset;
    JZ_U8 beta_offset;
}JZ_VPU_DBLK_CFG_S_T20;

/* RC cfg*/
typedef struct jzVPU_ATTR_H264_FIXQP_S_T20 {
    JZ_U32                    u32Gop;
    JZ_U8                     u8Fps;
    JZ_U8                     u8IQp;
    JZ_U8                     u8PQp;
}VPU_ATTR_H264_FIXQP_S_T20;

typedef struct jzVPU_ATTR_H264_CBR_S_T20 {
    JZ_U8                     u8Fps;
    JZ_U32                    u32Gop;
    JZ_U32                    u32Bitrate;                                        /*cbr target bitrate*/
}VPU_ATTR_H264_CBR_S_T20;

typedef struct jzVPU_PARAM_H264_CBR_S_T20 {
    JZ_U8                     u8MaxQp;                            /* the max qp value*/
    JZ_U8                     u8MinQp;                            /* the min qp value*/
    JZ_U8                     u8GopQpStep;                        /* the max qp value*/
    JZ_U8                     u8FrmQpStep;
    JZ_S8                     s8IQpBias;                          /* adjust i frame qp for i frame video quality */
    JZ_BOOL                   bGopRelat;
    JZ_U8                     u8NewMaxQpThr;                      /* the new max qp value when actual bitrate is too high*/
    JZ_FL                     fNewMaxQpTrigLvl;                   /* the ratio of allowed actual bitrate and target bitrate,then use the new max qp*/
    JZ_U32                    u32UseNewQpMaxAccumThr;
    JZ_U32                    u32SuperIFrmBitsThr;                /* the value of allowed i frame encode bits*/
    JZ_U32                    u32SuperPFrmBitsThr;                /* the value of allowed p frame encode bits*/
    JZ_U8                     u8MaxIprop;                         /* the max ratio of i frame and p frame,no use*/
    JZ_U8                     u8MinIprop;                         /* the min ratio of i frame and p frame,no use*/
    JZ_S8                     s8IPQpDlt;
    JZ_U8                     u8SkipFrmGaps;                      /* the max num of lost frame gaps*/
    JZ_U8                     u8SuperFrmReencTimes;               /* the max reencode times when current frame is super frame*/
    JZ_U8                     u8ReencTimes;                       /* the max reencode times when bits is big */
    JZ_FL                     fBeyondBpsThr;                      /* the ratio of actual bitrate and target bitrate*/
    VPU_RC_SuperFrmMode_S     superFrmMode;                       /* the process mode when current frame is super bits frame*/
    VPU_RC_BitRateUpMode_S    bitRateUpMode;                      /* the process mode when current gop bits is beyond target bitrate*/
    JZ_U8                     u8dropFrmNum;                       /* the num of dropped frames of last gop*/
    JZ_U8                     u8PskipFrmNum;                      /* the num of pskip frames of last gop*/
    JZ_BOOL                   bIsDropNextFrm;
    JZ_BOOL                   bIsSkipNextFrm;
}VPU_PARAM_H264_CBR_S_T20;

typedef struct jzVPU_ATTR_H264_VBR_S_T20 {
    JZ_U8                     u8Fps;
    JZ_U32                    u32Gop;
    JZ_U32                    u32StatTime;
    JZ_U32                    u32MaxBitrate;                      /*vbr max bitrate*/
}VPU_ATTR_H264_VBR_S_T20;

typedef struct jzVPU_PARAM_H264_VBR_S_T20 {
    JZ_U32                    u32MinBitrate;                      /* the min bitrate */
    JZ_U8                     u8MaxQp;                            /* the max qp value*/
    JZ_U8                     u8MinQp;                            /* the min qp value*/
    JZ_U8                     u8NewMaxQpThr;                         /* the new max qp value when actual bitrate is too high*/
    JZ_FL                     fNewMaxQpTrigLvl;                   /* the ratio of allowed actual bitrate and target bitrate,then use the new max qp*/
    JZ_U32					  u32UseNewQpMaxAccumThr;
    JZ_S8                     s8IQpBias;                          /* adjust i frame qp for i frame video quality */
    JZ_U8                     u8FrmQpStep;                        /* the max qp value difference between two successive p frames */
    JZ_U8                     u8GopQpStep;                        /* the max qp value difference between  i frame and other p frame */
    JZ_U8                     u8ChangePos;                        /* indicates the ratio of the current bitrate to the max bitrate when the scene is various*/
    JZ_BOOL                   bGopRelat;                          /* whether the successive gops allocated bits is relative*/
    JZ_U8                     u8SuperFrmReencTimes;               /* the max reencode times when current frame is super frame*/
    JZ_U8                     u8ReencTimes;                       /* the max reencode times when bits is big */
    JZ_FL                     fBeyondBpsThr;                      /* the ratio of actual bitrate and target bitrate*/
    JZ_U8                     u8DropFrmNum;                       /* the num of dropped frames of last gop*/
    JZ_U8                     u8PskipFrmNum;                      /* the num of pskip frames of last gop*/
    JZ_U8                     u8SkipFrmGaps;                      /* the max num of lost frame gaps*/
    JZ_U32                    u32SuperIFrmBitsThr;                /* the value of allowed i frame encode bits*/
    JZ_U32                    u32SuperPFrmBitsThr;                /* the value of allowed p frame encode bits*/
    JZ_U8                     u8MaxIprop;                         /* the max ratio of i frame and p frame,no use*/
    JZ_U8                     u8MinIprop;                         /* the min ratio of i frame and p frame,no use*/
    JZ_S8                     s8IPQpDlt;
    JZ_U8                     u8QualityLvl;                       /* video quality level,can set 0~4,default 2*/
    VPU_RC_SuperFrmMode_S     SuperFrmMode;                       /* the process mode when current frame is super bits frame*/
    VPU_RC_BitRateUpMode_S    BitRateUpMode;                      /* the process mode when current gop bits is beyond target bitrate*/
    JZ_BOOL                   bIsDropNextFrm;
    JZ_BOOL                   bIsSkipNextFrm;

}VPU_PARAM_H264_VBR_S_T20;

typedef struct jzVPU_ATTR_H264_SMART_S_T20 {
    JZ_U8                     u8Fps;
    JZ_U32                    u32Gop;
    JZ_U32                    u32StatTime;
    JZ_U32                    u32MaxBitrate;                      /*vbr max bitrate*/
}VPU_ATTR_H264_SMART_S_T20;

typedef struct jzVPU_PARAM_H264_SMART_S_T20 {
    JZ_U32                    u32MinBitrate;                      /* the min bitrate */
    JZ_U8                     u8MaxQp;                            /* the max qp value*/
    JZ_U8                     u8MinQp;                            /* the min qp value*/
    JZ_U8                     u8NewMaxQpThr;                         /* the new max qp value when actual bitrate is too high*/
    JZ_FL                     fNewMaxQpTrigLvl;                   /* the ratio of allowed actual bitrate and target bitrate,then use the new max qp*/
    JZ_U32					  u32UseNewQpMaxAccumThr;
    JZ_S8                     s8IQpBias;                          /* adjust i frame qp for i frame video quality */
    JZ_U8                     u8FrmQpStep;                        /* the max qp value difference between two successive p frames */
    JZ_U8                     u8GopQpStep;                        /* the max qp value difference between  i frame and other p frame */
    JZ_U8                     u8ChangePos;                        /* indicates the ratio of the current bitrate to the max bitrate when the scene is various*/
    JZ_BOOL                   bGopRelat;                          /* whether the successive gops allocated bits is relative*/
    JZ_U8                     u8SuperFrmReencTimes;               /* the max reencode times when current frame is super frame*/
    JZ_U8                     u8ReencTimes;                       /* the max reencode times when bits is big */
    JZ_FL                     fBeyondBpsThr;                      /* the ratio of actual bitrate and target bitrate*/
    JZ_U8                     u8DropFrmNum;                       /* the num of dropped frames of last gop*/
    JZ_U8                     u8PskipFrmNum;                      /* the num of pskip frames of last gop*/
    JZ_U8                     u8SkipFrmGaps;                      /* the max num of lost frame gaps*/
    JZ_U32                    u32SuperIFrmBitsThr;                /* the value of allowed i frame encode bits*/
    JZ_U32                    u32SuperPFrmBitsThr;                /* the value of allowed p frame encode bits*/
    JZ_U8                     u8MaxIprop;                         /* the max ratio of i frame and p frame,no use*/
    JZ_U8                     u8MinIprop;                         /* the min ratio of i frame and p frame,no use*/
    JZ_S8                     s8IPQpDlt;
    JZ_U8                     u8QualityLvl;                       /* video quality level,can set 0~4,default 2*/
    VPU_RC_SuperFrmMode_S     SuperFrmMode;                       /* the process mode when current frame is super bits frame*/
    JZ_BOOL                   bIsDropNextFrm;
    JZ_BOOL                   bIsSkipNextFrm;
}VPU_PARAM_H264_SMART_S_T20;

typedef struct{
  uint8_t roi_en;
  uint8_t roi_md;
  int8_t  roi_qp;
  uint8_t roi_lmbx;
  uint8_t roi_rmbx;
  uint8_t roi_umby;
  uint8_t roi_bmby;
}ROI_info;

typedef struct jzVPU_RC_S_T20 {
    /*cfg*/
    VPU_RC_MODE_S             rcMode;                             /* the mode of RC,can set CBR, VBR, FIXQP*/
    VPU_ATTR_H264_FIXQP_S_T20     attrH264Fixqp;                      /* FIXQP mode attribute parameter*/
    VPU_ATTR_H264_CBR_S_T20       attrH264Cbr;                        /* CBR mode attribut parametr*/
    VPU_ATTR_H264_VBR_S_T20       attrH264Vbr;                        /* VBR mode attribut parametr*/
    VPU_ATTR_H264_SMART_S_T20     attrH264Smart;                        /* SMART mode attribut parametr*/
    VPU_PARAM_H264_CBR_S_T20      paramH264Cbr;                       /* CBR mode high parametr*/
    VPU_PARAM_H264_VBR_S_T20      paramH264Vbr;                       /* VBR mode high parametr*/
    VPU_PARAM_H264_SMART_S_T20    paramH264Smart;                       /* SMART mode high parametr*/

    JZ_U8                     VPUCoreNum;                         /* VPU Core Num,set 1*/
    JZ_BOOL                   bIFrmReq;                           /* random require i frame*/
    JZ_BOOL                   bReEncEn;                           /* reencode en*/
    JZ_U8                     u8MbRCEn;                           /* mb level qp adjust en*/
    JZ_U8                     u8ChgRefEn;
    /*parameter*/
    JZ_U8                     FrameQP;                            /* curent frame qp from rc to VPU*/
    JZ_U8                     *Yplane;
    JZ_U32                    Ystride;
    JZ_U32                    MbWidth;
    JZ_U32                    MbHeight;
    JZ_U8                     FrameType;
    JZ_U32                    PredMad;                            /* VPU return frame mad*/
    JZ_U32                    ActualBits;                         /* VPU return frame actual bits */
    JZ_U32                    FrmCnt;
    JZ_U32                    u32MbInterCnt;
    JZ_U32                    u32MbInterMv;
    JZ_U32                    u32MbMvThr[3];
    JZ_U32                    u32MBSmoothThrV[6];
    JZ_U32                    u32MBSmoothThrH[6];
    JZ_U32                    u32MbSmoothCnt;

    JZ_U32                    u32MbSmoothCnt0;
    JZ_U32                    u32MbSmoothCnt1;
    JZ_U32                    u32MbSmoothCnt2;

    JZ_U32                    u32MbSmoothFlag[256*256];
    JZ_U8                     u8luma[256*256];
    JZ_S8                     s8mbQpOfst[7];
    JZ_U32                    u32MbQpTab[256*256];
    JZ_U32                    u32QPTabLen;
    JZ_U32                    u32QPTaben;
    JZ_U32                    u32MbQpAvg;
    JZ_U32                    u32MbSmoothPer;
    VPU_RC_SCENE_CONDITION_S  CurSceCndt;
    VPU_RC_SCENE_CONDITION_S  PrevSceCndt;
    VPU_RC_SCENE_CONDITION_S  LISceCndt;
    ROI_info                  *ROIInfo;
    void                      *RCptr0;
    void                      *RCptr1;                            /*Users don't care about its*/
    JZ_S8                     isBigPFrame;
    JZ_U8                     ChangeRef;
    JZ_U8 cost_bias_en;//ingenic
    JZ_U8 cost_bias_i_4x4;
    JZ_U8 cost_bias_i_16x16;
    JZ_U8 cost_bias_p_l0;
    JZ_U8 cost_bias_p_skip;
    JZ_U8 inter_nei_en;
    JZ_U8 skip_bias_en;
} VPU_RC_S_T20;

typedef struct JZ_VPU_ALL_BASE_T20 {
    JZ_U32 SCH;
    JZ_U32 AUX;
    JZ_U32 TCSM;
    JZ_U32 SRAM;
    JZ_U32 VDMA;
    JZ_U32 EFE;
    JZ_U32 MC;
    JZ_U32 DBLK;
    JZ_U32 VMAU;
    JZ_U32 SDE;
    JZ_U32 JPGC;
}JZ_VPU_ALL_BASE_T20;


void JZ_VPU_RC_VIDEO_CFG_T20(VPU_RC_S_T20 *rc);
void JZ_VPU_RC_FRAME_RC_T20(VPU_RC_S_T20 *rc);
int JZ_VPU_RC_FRAME_REPEATE_JUDGE_T20(VPU_RC_S_T20 *rc);
//void JZ_VPU_RC_MB_RC_Dlt(VPU_RC_S *rc);
//JZ_VPU_RC_FRAME_FINISH(VPU_RC_S *eprc);
//int JZ_VPU_ROI_SetRoiCfg( _H264E_SliceInfo *s, JZ_VPU_ROI_CFG_S *roicfg);
//void JZ_VPU_VDMA_Chain_Prepare_T20( _H264E_SliceInfo *s);
void JZ_VPU_RC_FREE_T20(VPU_RC_S_T20 *rc);
//void JZ_VPU_CABAC_Set( _H264E_SliceInfo *s, JZ_VPU_SDE_CFG_S *sdecfg);
//void JZ_VPU_DBLK_Set( _H264E_SliceInfo *s, JZ_VPU_DBLK_CFG_S *dblkcfg);
//void JZ_VPU_VMAU_Set( _H264E_SliceInfo *s, JZ_VPU_VMAU_CFG_S *vmaucfg);
//void JZ_VPU_Slice_GlobalSet( _H264E_SliceInfo *s, JZ_VPU_GLB_CFG_S *Glbcfg);

#endif //__JZM_ENC_API_H__
